﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace Hugo.UIPages
{
    public class PathSlidingIconUIPage : UIPage, Hugo.SlideIconCanvas.CarouselIndexChangedEventListener
    {
        private SlideIconCanvas sic;
        private Label currentSelectionLabel;
        private Label descriptionLabel;
        private List<UIOption> pages;
        //private int selectedIndex;

        public PathSlidingIconUIPage(String name, BitmapSource iconSource)
            : base(name, iconSource)
        {
            sic = new SlideIconCanvas(this);
            this.PageCanvas.Children.Add(sic);

            Grid g = new Grid();
            g.Width = GlobalSettings.EffectiveScreenMiddleX * 2;
            this.PageCanvas.Children.Add(g);

            descriptionLabel = new Label();
            descriptionLabel.Foreground = new SolidColorBrush(GlobalSettings.TextColor);
            descriptionLabel.FontSize = GlobalSettings.SelectedLabelFontSize;
            descriptionLabel.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            descriptionLabel.Margin = new Thickness(0, GlobalSettings.BreadcrumbPosition, 0, 0);
            

            currentSelectionLabel = new Label();
            currentSelectionLabel.Foreground = new SolidColorBrush(GlobalSettings.TextColor);
            currentSelectionLabel.FontSize = GlobalSettings.SelectedLabelFontSize;
            currentSelectionLabel.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            currentSelectionLabel.Margin = new Thickness(0, GlobalSettings.SelectedLabelPosition, 0, 0);

            g.Children.Add(descriptionLabel);
            g.Children.Add(currentSelectionLabel);
            pages = new List<UIOption>();
        }

        public void addUIOption(UIOption page)
        {
            pages.Add(page);
            sic.setUIOptions(pages);
        }


        public override bool respondToKey(KeyEventArgs e)
        {
            if (e.Key == Key.Left)
            {
                shiftLeft();
            }
            else if (e.Key == Key.Right)
            {

                shiftRight();
            }
            else if (e.Key == Key.Enter)
            {
                pushSelectedPage();
            }
            else
            {
                return false;
            }

            return true;
        }

        int indexOfSelectedImage = -1;

        public override bool respondToClick(MouseButtonEventArgs e) {

            //int indexOfImage = this.getIndexOfImageAtPosition(e.GetPosition(MainWindow.mainCanvas));
            
            if (indexOfSelectedImage != -1)
            {
                if (sic.getSelectedIndex() == indexOfSelectedImage)
                {
                    pages[indexOfSelectedImage].wasSelected();
                }
                else
                {
                    sic.setSelectedIndex(indexOfSelectedImage);
                }
                Console.WriteLine("User clicked on " + indexOfSelectedImage + " " + pages[indexOfSelectedImage].Name);
                return true;
            }

            return false;
        }

        public override void shiftLeft()
        {
            int newIndex = sic.getSelectedIndex() - 1;
            if (newIndex == -1) { newIndex = pages.Count - 1; }
            setSelectedIndex(newIndex);
        }

        public override void shiftRight()
        {
            int newIndex = sic.getSelectedIndex() + 1;
            if (newIndex == pages.Count) { newIndex = 0; }
            setSelectedIndex(newIndex);
        }

        public override void setPage()
        {
            descriptionLabel.Content = Translate.translate(this.getDescription());
            setSelectedIndex(1);
            base.setPage();
        }

        public void setSelectedIndex(int index)
        {
            sic.setSelectedIndex(index);
        }

        public void pushSelectedPage()
        {
            if (GlobalSettings.isBlind)
            {
                Sounds.speakMessage("Selecting");
            }

            UIOption selectedPage = getSelectedPage();
            selectedPage.wasSelected();
        }

        private UIOption getSelectedPage()
        {
            if (pages.Count == 0) { return null; }
            return pages[sic.getSelectedIndex()];
        }

        public void indexChanged(object o, SlideIconCanvas.CarouselIndexChangedEventArgs e)
        {
            String name = this.getSelectedPage().Name;
            if (this.getSelectedPage().CanTranslate)
            {
                name = Translate.translate(name);
            }
            this.currentSelectionLabel.Content = name;
        }
        DispatcherTimer _timer;

        public override bool setCursorPosition(System.Windows.Point p)
        {
            bool isHovering = false;
            bool wasHovering = HandCursor.getInstance().isHovering();

            indexOfSelectedImage = getIndexOfImageAtPosition(p);

            if (indexOfSelectedImage != -1)
            {
                isHovering = true;
            }

            //HandCursor.getInstance().setHovering(isHovering);

            if (wasHovering && !isHovering || _timer == null)
            {
                if (_timer != null) { _timer.Stop(); }

                 _timer = new DispatcherTimer();
                _timer.Interval = TimeSpan.FromMilliseconds(4000);
                _timer.Tag = this;
                _timer.Tick += new EventHandler(notificationTimerElapsed);
                _timer.Start();
            }

            return isHovering;
        }

        static void notificationTimerElapsed(object sender, EventArgs e)
        {
            //we want this event handler executed just once
            DispatcherTimer thisTimer = (DispatcherTimer)sender;
            PathSlidingIconUIPage page = (PathSlidingIconUIPage) thisTimer.Tag;
            if (!HandCursor.getInstance().isHovering())
            {
                //TODO:uncomment to autoscroll
                //page.shiftRight();
            }
        }

        private int getIndexOfImageAtPosition(Point p)
        {
            int index = -1;

            int i = 0;
            foreach (Image im in sic.getImages())
            {
                Point pos = im.PointToScreen(new Point());

                double rX = pos.X;
                double rY = pos.Y;
                double rX2 = rX + im.ActualWidth + HandCursor.handSize;
                double rY2 = rY + im.ActualHeight + HandCursor.handSize;

                if (rX < p.X && rY < p.Y && rX2 > p.X && rY2 > p.Y)
                {
                    index = i;
                    break;
                }
                i++;
            }

            indexOfSelectedImage = index;

            return index;
        }

        public void clearPage()
        {
            //throw new NotImplementedException();
        }
    }
}
